package api

import (
	"rank/internal/entity"
	"rank/internal/status"
	"rank/pkg/schema"

	"github.com/gin-gonic/gin"
)

// ranking godoc
//
//	@Summary	获取玩家名次
//	@Tags		Rank
//	@Accept		json
//	@Produce	json
//	@Param		pid	path		string									true	"玩家标识"	default(robot1)
//	@Success	200	{object}	schema.Response{data=schema.RankingRsp}	"响应体"
//	@Router		/v1/rank/ranking/{pid} [get]
func (r *restHandler) ranking(c *gin.Context) {
	pid := c.Param("pid")
	if len(pid) < 2 {
		r.respCode(c, status.InvalidParam)
		return
	}

	p, err := r.dao.Ranking(c.Request.Context(), pid)
	if err != nil {
		r.respDesc(c, -1, err.Error())
		return
	}

	r.respData(c, schema.RankingRsp{MyRanking: p.Ranking})
}

// notice godoc
//
//	@Summary	通知玩家数据变更
//	@Tags		Rank
//	@Accept		json
//	@Produce	json
//	@Param		Content-Type	header		string				true	"数据格式"	default(application/json)
//	@Param		data			body		schema.NoticeReq	true	"玩家信息"	default(schema.NoticeReq{pid="1001",level=100,value=66})
//	@Success	200				{object}	schema.Response		"响应体"
//	@Router		/v1/rank/notice [post]
func (r *restHandler) notice(c *gin.Context) {
	req := &schema.NoticeReq{}
	if err := c.ShouldBind(req); err != nil {
		r.respDesc(c, status.InvalidParam, err.Error())
		return
	}

	if err := r.dao.Notice(c.Request.Context(), &entity.Player{
		PID:   req.PID,
		Level: req.Level,
		Value: req.Value,
	}); err != nil {
		r.respDesc(c, -2, err.Error())
		return
	}

	r.respData(c, nil)
}

// partRank godoc
//
//	@Summary	局部榜单
//	@Tags		Rank
//	@Accept		json
//	@Produce	json
//	@Param		pid	path		string										true	"玩家标识" default(robot1)
//	@Success	200	{object}	schema.Response{data=schema.RankPageRsp}	"响应体"
//	@Router		/v1/rank/part-rank/{pid} [get]
func (r *restHandler) partRank(c *gin.Context) {
	pid := c.Param("pid")
	if pid == "" {
		r.respCode(c, status.LackParam)
		return
	}

	players, err := r.dao.PartRank(c.Request.Context(), pid)
	if err != nil {
		r.respDesc(c, -3, err.Error())
		return
	}

	r.respData(c, &schema.RankPageRsp{
		Total: len(players),
		List:  players,
	})
}

// list godoc
//
//	@Summary	分页查看榜单上玩家
//	@Tags		Rank
//	@Accept		json
//	@Produce	json
//	@Param		page		query		uint16										true	"页数"	default(1)
//	@Param		page-size	query		uint8										true	"每页容量"	default(10)
//	@Success	200			{object}	schema.Response{data=schema.RankPageRsp}	"响应体"
//	@Router		/v1/admin/rank/list [get]
func (r *restHandler) list(c *gin.Context) {
	req := &schema.RankPageReq{}
	if err := c.ShouldBindQuery(req); err != nil {
		r.respDesc(c, status.InvalidParam, err.Error())
		return
	}

	players, total, err := r.dao.PageMember(c.Request.Context(), int64(req.Page), int64(req.PageSize))
	if err != nil {
		r.respDesc(c, -4, err.Error())
		return
	}

	r.respData(c, &schema.RankPageRsp{
		Total: total,
		List:  players,
	})
}

// ban godoc
//
//	@Summary	榜单上拉黑某个玩家
//	@Tags		Rank
//	@Accept		json
//	@Produce	json
//	@Param		Content-Type	header		string			true	"数据格式"	default(application/json)
//	@Param		data			body		schema.BanReq	true	"玩家信息"	default(schema.NoticeReq{pid="1001",level=100,value=66})
//	@Success	200				{object}	schema.Response	"响应体"
//	@Router		/v1/admin/rank/ban [post]
func (r *restHandler) ban(c *gin.Context) {

}
